package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.entity.system.Dept;
import com.ms.p2p.provider.AdminUserProvider;
import com.ms.p2p.provider.RbacProvider;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 管理员控制器
 */
@Controller("AdminUserController")
@RequestMapping({"/admin/adminUser"})
public class AdminUserController extends AdminBaseController {

    @Reference
    private AdminUserProvider adminUserProvider;

    @Reference
    private RbacProvider rbacProvider;



    @RequestMapping("/save")
    @ResponseBody
    public Message save(@ModelAttribute AdminUser adminUser) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {

        if (StringUtils.isNotBlank(adminUser.getId()+"")) {
            AdminUser old = adminUserProvider.findById(adminUser.getId()+"");
            adminUser.setPassword(old.getPassword());
            adminUser.setAddtime(old.getAddtime());
            adminUserProvider.update(adminUser);

        }else {
            if (adminUserProvider.selectByadmin_name(adminUser.getAdmin_name()) != null) {
                return Message.error("登录名重复！");
            }else{
                adminUser.setAddtime(MsDateUtils.getTime());
                adminUserProvider.insert(adminUser);
            }
        }
       return Message.success("操作成功！");

    }

    @RequestMapping("/toMyInformation")
    public String toMyInformation(HttpServletRequest request,
                                  HttpSession session) {
        AdminUser emp = getCurrentEmp();
        if (emp != null) {
            AdminUser empDteail = adminUserProvider.findById(String.valueOf(emp.getId()));

            List<Dept> dplist = rbacProvider.findDepts();
            request.setAttribute("dplist", dplist);

            request.setAttribute("empDteail", empDteail);
        }
        return "admin/user/info";
    }

    /**
     * 修改密码
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/setPwd")
    @ResponseBody
    public Message setPwd(HttpServletRequest request, HttpSession session)
            throws IntrospectionException, IllegalAccessException,
            InvocationTargetException {
        AdminUser emp = getCurrentEmp();
        String oldPassword = request.getParameter("oldPassword");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        if (oldPassword == null) {
            return Message.error("请输入原密码");
        } else if (password1 == null) {
            return Message.error("请输入新密码");
        } else if (!(password1.equals(password2))) {
            return Message.error("两次密码不一致");
        } else if (!DigestUtils.md5Hex(oldPassword).equals(emp.getPassword())) {
            return Message.error("原密码错误");
        } else {
            emp.setPassword(DigestUtils.md5Hex(password1));
            if (adminUserProvider.update(emp) < 1) {
                return Message.error("操作失败");
            } else {
                return Message.success("操作成功");
            }
        }
    }


    @RequestMapping("/pwd")
    public String modifyPwd() {
        return "/admin/system/pwd";
    }

    @RequestMapping("/modify2")
    public String queryById2(HttpServletRequest request) {
        String returnURL = "";
        AdminUser e = new AdminUser();
        e = adminUserProvider.findById(request.getParameter("uid"));
        request.setAttribute("employeeModify", e);
        returnURL = "manage/departs/add&modify";
        return returnURL;
    }


    @Override
    public boolean getBinderFlag() {
        return true;
    }
}